package ua.lviv.franko.solvers.stressanalysis.twoDimention;

import java.util.ArrayList;

import org.jzy3d.maths.Range;
import org.jzy3d.plot3d.builder.Mapper;

import ua.lviv.franko.mlsFunctions.twoDimension.BaseFunction;
import ua.lviv.franko.mlsFunctions.twoDimension.Particle;
import ua.lviv.franko.mlsFunctions.twoDimension.Values;
import ua.lviv.franko.visualisation.Visualise3D;

/**
 * @author marko
 * 
 */
public class EFGResult2D {
	protected ArrayList<BaseFunction>	baseFunctions;
	protected ArrayList<Particle>		particles;
	protected double[]					res;

	public EFGResult2D(double[] res, ArrayList<BaseFunction> baseFunctions) {
		this.baseFunctions = baseFunctions;
		this.res = res;
	}

	public EFGResult2D(double[] res, ArrayList<BaseFunction> baseFunctions, ArrayList<Particle> particles) {
		this.baseFunctions = baseFunctions;
		this.res = res;
		this.particles = particles;
	}

	public void displayU(Range xrange, Range yrange) {
		Visualise3D.show(new Mapper() {

			public double f(double x, double y) {
				if (particles != null) {
					double tmp = 0.0;
					for (int i = 0; i < baseFunctions.size(); i++) {
						BaseFunction bf = baseFunctions.get(i);
						Particle p = bf.getXi();
						if (p.lengthTo(new Particle(x, y, 0.0)) < p.getRho()) {
							Values[] v = bf.calculateAllFunctions(x, y);
							for (int j = 0; j < v.length; j++) {
								tmp += v[j].val * res[2 * particles.indexOf(bf.getNodeList().get(j))];
							}
							System.out.println(x + "\t" + y + "\t" + tmp);
							return tmp;
						}
					}

					return 0.0;
				} else {
					double tmp = 0.0;
					for (int i = 0; i < baseFunctions.size(); i++) {
						BaseFunction bFunction = baseFunctions.get(i);
						// tmp+=bFunction.calculateDerivatyY(x, y)*res[i];
						tmp += bFunction.calculate(x, y) * res[2 * i];
					}
					System.out.println(x + "\t" + y + "\t" + tmp);
					return tmp;// - (-5*(x*x) + 5*x);
				}
			}

		},xrange, yrange);
	}

	public void displayV(Range xrange, Range yrange) {
		Visualise3D.show(new Mapper() {

			public double f(double x, double y) {
				if (particles != null) {
					double tmp = 0.0;
					for (int i = 0; i < baseFunctions.size(); i++) {
						BaseFunction bf = baseFunctions.get(i);
						Particle p = bf.getXi();
						if (p.lengthTo(new Particle(x, y, 0.0)) < p.getRho()) {
							Values[] v = bf.calculateAllFunctions(x, y);
							for (int j = 0; j < v.length; j++) {
								tmp += v[j].val * res[2 * particles.indexOf(bf.getNodeList().get(j)) + 1];
							}
							System.out.println(x + "\t" + y + "\t" + tmp);
							return tmp;
						}
					}

					return 0.0;
				} else {
					double tmp = 0.0;
					for (int i = 0; i < baseFunctions.size(); i++) {
						BaseFunction bFunction = baseFunctions.get(i);
						// tmp+=bFunction.calculateDerivatyY(x, y)*res[i];
						tmp += bFunction.calculate(x, y) * res[2 * i + 1];
					}
					System.out.println(x + "\t" + y + "\t" + tmp);
					return tmp;// - (-5*(x*x) + 5*x);
				}
			}

		},xrange, yrange);
	}
}
